package cn.dnui_wnx725.dnuifood_16110100725.controller;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import java.util.List;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.adapter.FoodByShopAdapter;
import cn.dnui_wnx725.dnuifood_16110100725.bean.FoodBean;
import cn.dnui_wnx725.dnuifood_16110100725.bean.RegistBean;
import cn.dnui_wnx725.dnuifood_16110100725.listener.ListListener;
import cn.dnui_wnx725.dnuifood_16110100725.listener.TListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.CollectShopModel;
import cn.dnui_wnx725.dnuifood_16110100725.model.FoodByShopModel;

public class FoodActivity extends BaseActivity {
    private String shop_id;
    private RecyclerView recyclerView;
    private Context context;
    private RecyclerView.LayoutManager linearLayoutManager;
    private Button btn_favor_shop;
    private SwipeRefreshLayout mSwipeLayout;


    ListListener<FoodBean> listListener = new ListListener<FoodBean>() {

        @Override
        public void onResponse(final List<FoodBean> list) {
            final FoodByShopAdapter foodByShopAdapter = new FoodByShopAdapter(FoodActivity.this, list, R.layout.item);
            FoodByShopAdapter.OnItemClickListener onItemClickListener = new FoodByShopAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(FoodActivity.this, FoodDetailActivity.class);
                    intent.putExtra("foodname", list.get(position).getFoodname());
                    intent.putExtra("foodintro", list.get(position).getIntro());
                    intent.putExtra("foodprice", list.get(position).getPrice());
                    intent.putExtra("foodimg", list.get(position).getPic());
                    intent.putExtra("foodid", list.get(position).getFood_id());
                    startActivity(intent);
                }
            };
            foodByShopAdapter.setList(list);
            foodByShopAdapter.setmOnItemClickListener(onItemClickListener);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(foodByShopAdapter);
            mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    foodByShopAdapter.setList(list);
                    foodByShopAdapter.notifyDataSetChanged();
                    mSwipeLayout.setRefreshing(false);
                }
            });
        }

        @Override
        public void onFail(String msg) {

        }

        ;
    };
    TListener<RegistBean> tListener = new TListener<RegistBean>() {
        @Override
        public void onResponse(RegistBean registBean) {

            showToast("操作成功");
        }

        @Override
        public void onFail(String msg) {
            showToast("操作失败");
        }
    };

    @Override
    void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_foodbyshop);
        btn_favor_shop = (Button) findViewById(R.id.btn_favor_shop);
        linearLayoutManager = new LinearLayoutManager(FoodActivity.this);
    }

    @Override
    void initEvent() {
        FoodByShopModel foodByShopModel = new FoodByShopModel();
        foodByShopModel.getFoodMessage(shop_id, listListener);

    }

    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);
//        Intent intent=getIntent();
        shop_id = getIntent().getStringExtra("shopid");
        initView();
        initEvent();
        initData();
        mSwipeLayout = findViewById(R.id.id_swipe_ly);
        mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        btn_favor_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CollectShopModel collectShopModel = new CollectShopModel();
                collectShopModel.collect(getUserID(), shop_id, tListener);
            }
        });
    }

    private void request_list() {
        FoodByShopModel foodByShopModel = new FoodByShopModel();
        foodByShopModel.getFoodMessage(shop_id, listListener);
    }

    @Override
    public void onResume() {
        request_list();
        super.onResume();
    }

    void onBackpress() {
        super.onBackPressed();
    }
}

